package com.leetroll.app.controller;

import com.leetroll.api.service.CommentService;
import com.leetroll.api.service.IssueService;
import com.leetroll.common.bean.ApiResponse;
import com.leetroll.common.entity.Comment;
import com.leetroll.common.entity.Issue;
import com.leetroll.common.entity.Order;
import com.leetroll.common.utils.UserUtil;
import com.leetroll.common.vo.CommentVO;
import com.leetroll.common.vo.OrderDetailVO;
import com.leetroll.common.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/comment")
@Api(value = "问题相关接口", tags = {"问题相关接口"})
@Slf4j
public class CommentController {

    private final CommentService commentService;

    @Autowired
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @ApiOperation(value = "查询评论", notes = "查询评论")
    @GetMapping("/list")
    public ApiResponse<List<CommentVO>> list(@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                         @RequestParam(name = "pageSize", required = false, defaultValue = "12") Integer pageSize,
                                         @RequestParam(name = "rescId", required = false) Long rescId,
                                         @RequestParam(name = "rescType", required = false) String rescType
    ) {
        List<CommentVO> list = commentService.list(rescType, rescId, pageNumber, pageSize);
        return ApiResponse.OK(list);
    }

    @ApiOperation(value = "查询评论数量", notes = "查询评论数量")
    @GetMapping("/count")
    public ApiResponse<Integer> count(@RequestParam(name = "rescId", required = false) Long rescId,
                                             @RequestParam(name = "rescType", required = false) String rescType) {
        Integer size = commentService.count(rescType, rescId);
        return ApiResponse.OK(size);
    }

    @ApiOperation(value = "新增评论", notes = "新增评论")
    @PostMapping("/add")
    public ApiResponse<?> add(@RequestBody Comment comment) {
        comment.setUserId(UserUtil.getUserId());
        commentService.save(comment);
        return ApiResponse.OK();
    }
}
